<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/16
 * Time: 9:41
 */

namespace app\home\controller;


class Bet extends common
{
    public function betPage(){
        //用户信息
        $user = session('user');
        if(!$user){
            $this->error('账号未登录!','login/login');
        }
        $data['user_pic'] = $user['user_pic'];
        $data['name'] = $user['name'];
        //最近一期的开奖信息
        $ssc = db('ssc')
            ->where('opencode','--')
            ->where('expect','like',date('Ymd').'%')
            ->order('expect ASC')
            ->limit(1)
            ->find();
        //夺宝记录
        $bet = db('bet')->where('uid',$user['id'])->order('id desc')->limit(20)->select();
        $this->assign('bet',$bet);
        $this->assign('user',$data);
        $this->assign('code',$ssc);
        return $this->fetch('common/duobao');
    }

    public function duobao10(){
        //用户信息
        $user = session('user');
        if(!$user){
            $this->error('账号未登录!','login/login');
        }
        $data['user_pic'] = $user['user_pic'];
        $data['name'] = $user['name'];
        //最近一期的开奖信息
        $ssc = db('ptc')
        ->where('opencode','--')
        ->where('expect','like',date('Ymd').'%')
        ->order('expect ASC')
        ->limit(1)
        ->find();
        //夺宝记录
        $bet = db('bet')->where('uid',$user['id'])->order('id desc')->limit(20)->select();
        $this->assign('bet',$bet);
        $this->assign('user',$data);
        $this->assign('code',$ssc);
        return $this->fetch('common/duobao10');
    }
    
    /**
     * 投注数据提交
     * @return string
     */
    public function betData(){
        $data =input('post.');
        if(!$data){
            return json_encode(['msg'=>0,'info'=>'无数据']);
        }
        $uid = session('user')['id'];
        //判断用户余额
        $codemoney = db('webconfig')->where('id',1)->value('code_money');
        $summoney = $data['zs'] * $codemoney;
        if(db('user')->where('id',$uid)->value('money') >= $summoney){
            db('user')->where('id',$uid)->setDec('money',$summoney);
            //向彩票数据库插入投注金额
            if($data['type'] == 1){
                db('ptc')->where('expect',$data['expect'])->setInc('summoney',$summoney);
            }elseif ($data['type'] == 2){
                db('ssc')->where('expect',$data['expect'])->setInc('summoney',$summoney);
            }


            //返点计算



        }else{
            return json_encode(['msg'=>0,'info'=>'可用余额不足!']);
        }
        $type = $data['type'];
        $expect = $data['expect'];
        $info['uid'] = $uid;
        $info['expect'] = $expect;
        $info['code_type'] = $type;
        $info['time'] = date('Y-m-d h:i:s');
        foreach ($data['haoma'] as $k=>$v){
            if($data['weizi'][$k] == '个位'){
                $info['seat'] = 1;
            }elseif($data['weizi'][$k] == '十位'){
                $info['seat'] = 2;
            }elseif($data['weizi'][$k] == '百位'){
                $info['seat'] = 3;
            }elseif($data['weizi'][$k] == '千位'){
                $info['seat'] = 4;
            }elseif($data['weizi'][$k] == '万位'){
                $info['seat'] = 5;
            }
            $info['content'] = $v;
            $info['number'] =$data['zhushu'][$k];
            db('bet')->insert($info);
        }
        return json_encode(['msg'=>1,'info'=>'下注成功!']);
    }

    /**
     * ajax轮询接收数据
     */
    public function ajax(){
        $expect = input('post.expect');
        $type = input("type","",'intval');
        $db = $type?db('ptc'):db('ssc');
        //判断是否已经开奖
        if($db->where('expect',$expect)->value('opencode') == '--'){
            $qishu = substr($expect,-3);
            $riqi = substr($expect,0,8);
            //拿到昨天当前期数的开奖时间
            $yexpect = date('Ymd', strtotime($riqi)-3600*24).$qishu;
            $ytime = $db->where('expect',$yexpect)->value('opentime');
            //对比当前时间(提前30秒不能下注)
            if(strtotime($ytime)+3600*24-time() > 60){
                return json_encode(['msg'=>0,'info'=>'可以下注']);
            }else{
                return json_encode(['msg'=>1,'info'=>'正在开奖']);
            }
        }else{
            //最近一期的要开奖信息
            $ssc = $db
                ->where('opencode','--')
                ->where('expect','like',date('Ymd').'%')
                ->order('expect ASC')
                ->limit(1)
                ->find();
            return json_encode(['msg'=>2,'info'=>'已经开奖','expect'=>$ssc['expect']]);
        }
    }
}